/**
 * 
 */
package net.sf.rose.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import json.JsonHttpResponse;

import net.sf.rose.exception.SystemException;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author fengjian
 * @since 2013-5-9
 */
public class ExceptionHandler implements HandlerExceptionResolver {

	private Logger logger = Logger.getLogger(ExceptionHandler.class);

	/**
	 * 功能：处理所有的异常，给最终用户显示简单的异常消息。
	 */
	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
		String message = null;
		if (!(e instanceof SystemException)) {
			logger.warn("发生未捕获的异常！", e);
			message = e.toString();
		} else {
			SystemException s = (SystemException) e;
			message = s.getErrorString() + "：" + e.toString();
		}
		JsonHttpResponse.renderJson(response, "<font size='2px'><B>系统发生错误，请查看错误提示或联系管理员！<br>错误信息：</B>" + message +"</font>");
		return null;
	}

}
